﻿// <copyright file="PlayerListPanel.xaml.cs" company="NorthCoast NERO">
//     NorthCoast NERO. All rights reserved.
// </copyright>
// <author>Eric Kociecki</author>
namespace NcnPlayerCheckIn.Controls
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Globalization;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Data;
    using System.Windows.Documents;
    using System.Windows.Input;
    using System.Windows.Media;
    using System.Windows.Media.Imaging;
    using System.Windows.Navigation;
    using System.Windows.Shapes;

    /// <summary>
    /// Interaction logic for CheckInPanel.xaml
    /// </summary>
    public partial class PlayerListPanel : UserControl
    {
        /// <summary>
        /// Initializes a new instance of the PlayerListPanel class.
        /// </summary>
        public PlayerListPanel()
        {
            this.InitializeComponent();
        }

        /// <summary>
        /// Gets or sets the object reference to the parent window.  This allows access to class members that are shared between all of the custom panels.
        /// </summary>
        public MainWindow ParentWindow { get; set; }

        /// <summary>
        /// Gets a list of all Players that have checked in at this event (some may have also checked out, but they are still tracked here).  This is a pass through to the MainWindow.PlayerList object.
        /// </summary>
        public Collection<Player> PlayerList
        {
            get { return this.ParentWindow.PlayerList; }
        }

        /// <summary>
        /// Updates the list of players in the GUI when changes are made.  TODO this is horribly inefficient.  Every time the method is called it erases everything and redraws it all.  Look into better solutions.
        /// </summary>
        public void UpdatePlayerDisplay()
        {
            // removes all playerDisplay objects from this panel
            this.playerPanel.Children.Clear();

            // this is used to track alternating lines so that they can have different colored backgrounds
            bool isAlternate = false;

            foreach (Player p in this.ParentWindow.PlayerList)
            {
                // add each player back in to this panel
                this.DisplayPlayer(p, isAlternate);

                isAlternate = !isAlternate;
            }

            // add one blank PlayerDisplay at the bottom for adding new players
            this.DisplayPlayer(null, isAlternate);
        }

        /// <summary>
        /// Creates a PlayerDisplay, adds a Player to it, and adds it to this panel.
        /// </summary>
        /// <param name="newPlayer">This is the player to add.  This can be null, in which case a blank PlayerDisplay will be created and put in to edit mode.</param>
        /// <param name="isAlternate">Determines coloring by making alternating lines different colors.  Adjacent PlayerDisplay objects should have non-equal IsAlternate values.</param>
        public void DisplayPlayer(Player newPlayer, bool isAlternate)
        {
            PlayerDisplay newPlayerDisplay = new PlayerDisplay();

            newPlayerDisplay.ManagerControl = this;
            newPlayerDisplay.IsAlternate = isAlternate;

            if (newPlayer == null)
            {
                // if this is a blank PlayerDisplay, put it in edit mode
                newPlayerDisplay.EnterEditMode();
            }
            else
            {
                newPlayerDisplay.Player = newPlayer;
            }

            this.playerPanel.Children.Add(newPlayerDisplay);
        }

        /// <summary>
        /// Exits edit mode on all PlayerDisplay objects in this panel.
        /// </summary>
        /// <param name="displayToKeepOpen">A single PlayerDisplay that should remain in EditMode.</param>
        public void ExitEditMode(PlayerDisplay displayToKeepOpen = null)
        {
            foreach (PlayerDisplay pd in this.playerPanel.Children)
            {
                if (pd != displayToKeepOpen)
                {
                    pd.ExitEditMode();
                }
            }
        }
    }
}
